package com.comdo.newup_loan.module.repayment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.adapter.RepayDetailAdapter;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.model.RePayDetailBean;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.MyItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by liyuan on 2017/9/29.
 */

public class RePaymentDetailActivity extends BaseActivity<RepayDetailPersenter> implements RepayDetailContact.View{
    @BindView(R.id.main_srl)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    private RepayDetailAdapter mAdapter;
    private List<RePayDetailBean.RePayPlan> listData;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_repayment_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        new TitleMenuUtil(RePaymentDetailActivity.this,"还款计划",true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(RePaymentDetailActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new MyItemDecoration());

        listData = new ArrayList<RePayDetailBean.RePayPlan>();
        mAdapter = new RepayDetailAdapter(listData,RePaymentDetailActivity.this);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getList(getIntent().getIntExtra("tenderId",0), Config.loginInfo.getUserId());
            }
        });
    }

    @Override
    protected void init() {
        super.init();

    }

    @Override
    public RepayDetailPersenter getmPresenter() {
        return new RepayDetailPersenter();
    }

    @Override
    public void presenterSetView() {
        if (mPresenter != null)
            mPresenter.setView(this);
    }

    @Override
    public void getFail(String failMessage) {
        showToast(failMessage);
    }

    @Override
    public void getList(RePayDetailBean datas) {
        if(mRefreshLayout.isRefreshing())
            mRefreshLayout.setRefreshing(false);
        listData.clear();
        listData.addAll(datas.getRepayPlan());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getList(getIntent().getIntExtra("tenderId",0), Config.loginInfo.getUserId());
    }
}
